package com.neuedu.mvc.controller;

import com.neuedu.dao.DeptDao;
import com.neuedu.entity.Dept;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Objects;

/**
 * http://localhost:8080/web/dept?_type=list
 */
@WebServlet(urlPatterns = "/dept")
public class DeptController extends HttpServlet {




    private DeptDao deptDao = new DeptDao();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //针对的post
        req.setCharacterEncoding("utf-8");


        //有具体的业务代码 CRUD
        String type = req.getParameter("_type");
        //查询列表
        if(Objects.isNull(type) || "list".equals(type)){
            this.queryList(req,resp);
        }
        //新增
        else if("save".equals(type)){
            this.save(req,resp);
        }

        //修改q=前查询
        else if("toEdit".equals(type)){
            this.toEdit(req,resp);
        }
        //修改
        else if("update".equals(type)){
            this.update(req,resp);
        }

        //删除
        else if("deleteById".equals(type)){
            this.deleteById(req,resp);
        }


    }

   
    /**
     * 根据主键删除一条
     * @param req
     * @param resp
     */
    private void deleteById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //获取主键
        String deptno = req.getParameter("deptno");

        //删除
        int count = deptDao.deleteById(Integer.parseInt(deptno));
        //重定向 跳转到查询List
        resp.sendRedirect(req.getContextPath()+"/dept?_type=list");
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        //设置请求编码

        String deptno = req.getParameter("deptno");
        String dname = req.getParameter("dname");
        String loc = req.getParameter("loc");

        Dept dept = new Dept(
                Integer.parseInt(deptno),
                dname,
                loc
        );

        DeptDao deptDao = new DeptDao();
        int update = deptDao.updateById(dept);

        //重定向 list
        //获取上下文
        String contextPath = req.getContextPath();
        resp.sendRedirect(contextPath+"/dept?_type=list");
    }

    private void toEdit(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String where  = " where deptno = ?";
        String deptno = request.getParameter("deptno");
        Dept dept = deptDao.selectOne(where, deptno);

        //dept 设置到request
        request.setAttribute("dept",dept);

        //内部跳转到修改页面
        request.getRequestDispatcher("/business/dept/dept_edit.jsp").forward(request,response);

    }

    private void save(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String deptno = req.getParameter("deptno");
        String dname = req.getParameter("dname");
        String loc = req.getParameter("loc");

        Dept dept = new Dept(
                Integer.parseInt(deptno),
                dname,
                loc
        );

        DeptDao deptDao = new DeptDao();
        int insert = deptDao.insert(dept);

        //重定向 list
        //获取上下文
        String contextPath = req.getContextPath();
        resp.sendRedirect(contextPath+"/dept?_type=list");
    }

    private void queryList(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String where  = "  ";
        List<Dept> deptList = deptDao.selectList(where);

        //把depts交给 jsp使用渲染  列表
        //通过 request共享数据
        request.setAttribute("deptList",deptList);

        //src/main/webapp/business/dept/dept_list.jsp
        request.getRequestDispatcher("/business/dept/dept_list.jsp").forward(request,response);

    }


    /**
     * 输出响应内容(html)
     * @param response
     * @param html
     */
    public void out(HttpServletResponse response ,String html) throws IOException {
        response.setContentType("text/html;charset=utf8");
        response.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();
        out.write(html);
        out.flush();
        out.close();
    }


}
